using System;
using System.Collections.Generic;
using Commons.Models.Definitions;

namespace Commons.Models.Stats
{
	public class LeveledStat : SimpleStat
	{
		private int initialNum;

		public LeveledStatDefinition LeveledStatDefinition
		{
			get
			{
				return (LeveledStatDefinition)Definition;
			}
		}

		public int Progress
		{
			get
			{
				LevelDefinition thisLevel = LeveledStatDefinition.GetLevelByValue(Value);
				LevelDefinition nextLevel = LeveledStatDefinition.GetLevelByNum(thisLevel.Num + 1);

				if (nextLevel == null || nextLevel.Required <= thisLevel.Required)
				{
					return 0;
				}

				return (int) Math.Floor((Value - thisLevel.Required) * 100d / (nextLevel.Required - thisLevel.Required));
			}
		}

		public LeveledStat(LeveledStatDefinition definition)
			: this(definition, 0)
		{

		}

		public LeveledStat(LeveledStatDefinition definition, int value)
			: base(definition, value)
		{
			this.initialNum = GetLevel().Num;
		}

		public override bool CanSubstract(int value)
		{
			return false;
		}

		public LevelDefinition GetLevel()
		{
			return LeveledStatDefinition.GetLevelByValue(Value);
		}

		public LevelDefinition GetLevelByValue(int value)
		{
			return LeveledStatDefinition.GetLevelByValue(value);
		}

		public int GetRequiredValue(int num)
		{
			return LeveledStatDefinition.GetLevelByNum(num).Required;
		}

		public IEnumerable<LevelDefinition> GetUpdatedLevels(bool update)
		{
			int currentNum = GetLevel().Num;
			int size = Math.Max(currentNum - initialNum, 0);

			IList<LevelDefinition> levels = new List<LevelDefinition>(size);

			for (int num = initialNum + 1; num <= currentNum; num++)
			{
				levels.Add(LeveledStatDefinition.GetLevelByNum(num));
			}

			if (update)
			{
				initialNum = currentNum;
			}

			return levels;
		}
	}
}
